<html>
<head><meta charset="utf-8"><title>How to get Ty&#x27;s span in Pat · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20Ty&#x27;s.20span.20in.20Pat.html">How to get Ty&#x27;s span in Pat</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="233109473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20Ty%27s%20span%20in%20Pat/near/233109473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> GuillaumeGomez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20Ty&#x27;s.20span.20in.20Pat.html#233109473">(Apr 04 2021 at 20:36)</a>:</h4>
<p>I'm trying to get a <code>Pat</code>'s type (not the ident!) span from <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.PatKind.html">PatKind</a>::Binding. The problem is that it seems like I can only get the span for the ident, which is not what I'm looking for. For more context, I'm trying to make a connection between a type used in code and its definition. For the definition, no problem, I can look into the <code>FnDecl</code> from the input types, however I still need to be able to get the span from the <code>Body</code> and it doesn't like I can get it.</p>
<p>If anyone has an idea?</p>
<p>Thanks in advance!</p>



<a name="233109701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20Ty%27s%20span%20in%20Pat/near/233109701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20Ty&#x27;s.20span.20in.20Pat.html#233109701">(Apr 04 2021 at 20:41)</a>:</h4>
<p>you can't get a span from the type itself, the same type can be used in many different places</p>



<a name="233109705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20Ty%27s%20span%20in%20Pat/near/233109705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20Ty&#x27;s.20span.20in.20Pat.html#233109705">(Apr 04 2021 at 20:41)</a>:</h4>
<p>where are you getting the type from? that might have a span</p>



<a name="233109719"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20Ty%27s%20span%20in%20Pat/near/233109719" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20Ty&#x27;s.20span.20in.20Pat.html#233109719">(Apr 04 2021 at 20:41)</a>:</h4>
<p>yeah, Pat has one: <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/struct.Pat.html">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/struct.Pat.html</a></p>



<a name="233120029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20Ty%27s%20span%20in%20Pat/near/233120029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> GuillaumeGomez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20Ty&#x27;s.20span.20in.20Pat.html#233120029">(Apr 04 2021 at 23:24)</a>:</h4>
<p>That's what I said: the <code>Pat</code> has  span for the ident. For example in <code>x: u32</code>, the span in the <code>Pat</code> is for <code>x</code>, I want the one for <code>u32</code>. :)</p>
<p>Currently, I'm looping through functions' input parameters to get the type definition span, and through the <code>Pat</code> to get the "type" span (the one which appears in the function definition).</p>



<a name="233120328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20Ty%27s%20span%20in%20Pat/near/233120328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20Ty&#x27;s.20span.20in.20Pat.html#233120328">(Apr 04 2021 at 23:30)</a>:</h4>
<p>that's not part of the pattern</p>



<a name="233165791"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20Ty%27s%20span%20in%20Pat/near/233165791" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> GuillaumeGomez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20Ty&#x27;s.20span.20in.20Pat.html#233165791">(Apr 05 2021 at 12:30)</a>:</h4>
<p>How could I get this information then? Another way to do so?</p>



<a name="233173457"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20Ty%27s%20span%20in%20Pat/near/233173457" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20Ty&#x27;s.20span.20in.20Pat.html#233173457">(Apr 05 2021 at 13:45)</a>:</h4>
<p>This is what I inferred by looking at the docs:</p>
<ul>
<li>If it's for a statement, the type is included in the <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/struct.Local.html"><code>Local</code></a></li>
<li>If it's a function parameter, <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/struct.FnDecl.html#structfield.inputs"><code>FnDecl::inputs</code></a> contains the type of all the parameters in a slice</li>
</ul>



<a name="233173635"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20Ty%27s%20span%20in%20Pat/near/233173635" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20Ty&#x27;s.20span.20in.20Pat.html#233173635">(Apr 05 2021 at 13:46)</a>:</h4>
<p>That gives you <code>hir::Ty</code>, which contains a <code>span</code> field, but I'm not sure of what that span points to</p>



<a name="233176943"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20Ty%27s%20span%20in%20Pat/near/233176943" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> GuillaumeGomez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20Ty&#x27;s.20span.20in.20Pat.html#233176943">(Apr 05 2021 at 14:15)</a>:</h4>
<p>I used instead a Visitor with <code>visit_ty</code> and it seems to provide me all the information I'm looking for. :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>